//
//  BaiKeSecondClassesItemScrollView.m
//  BaseProject
//
//  Created by mac on 2020/12/22.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "BaiKeSecondClassesItemScrollView.h"
#import "BaiKeMainClassesModel.h"
@interface BaiKeSecondClassesItemScrollView ()
@property(nonatomic,strong)UIScrollView *mainView;
@property(nonatomic,strong)NSMutableArray<UILabel *> *itemsArray;
@property(nonatomic,strong)UIView *lineView;
@property(nonatomic,strong)UILabel *preLab;
@end

@implementation BaiKeSecondClassesItemScrollView
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = WhiteColor;
        [self mainView];
        //[self lineView];
    }
    return self;
}
- (void)setListModel:(BaiKeMainClassesDataModel *)listModel
{
    _listModel = listModel;
    [self.itemsArray enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    [self.itemsArray removeAllObjects];
    CGFloat orinX = 14*ADAPTER_WIDTH,marginX = orinX;
    for (NSInteger index = 0; index < listModel.sysClassificationList.count; index ++) {
        BaiKeMainClassesDataModel *threeModel = listModel.sysClassificationList[index];
        CGFloat itemWidth = [threeModel.threeName sizeWithFont:kFontRegular13].width + 22*ADAPTER_WIDTH;
        UILabel *lab = [[UILabel alloc]init];
        [self.mainView addSubview:lab];
        lab.text = threeModel.threeName;
        lab.font = kFontRegular13;
        lab.textAlignment = NSTextAlignmentCenter;
        lab.layer.cornerRadius = 13*ADAPTER_WIDTH;
        lab.layer.masksToBounds = YES;
        lab.frame = CGRectMake(orinX, 4*ADAPTER_WIDTH, itemWidth, 26*ADAPTER_WIDTH);
        orinX = (index == listModel.sysClassificationList.count - 1) ? lab.right + 6*ADAPTER_WIDTH : lab.right + marginX;
        if (threeModel.isSelected) {
            lab.backgroundColor = rgba(255, 240, 240, 1);
            lab.textColor = rgba(255, 66, 66, 1);
            self.preLab = lab;
        } else {
            lab.backgroundColor = rgba(245, 245, 245, 1);
            lab.textColor = rgba(102, 102, 102, 1);
        }
        
        [self.itemsArray addObject:lab];
        lab.tag = index;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickItemLab:)];
        lab.userInteractionEnabled = YES;
        [lab addGestureRecognizer:tap];
    }
    self.mainView.contentSize = CGSizeMake(MAX(self.mainView.width + 1, orinX), 0);
    
    //调整位移
    [self adjustOffsetX];
}
- (void)nativeJumpScrollTopView
{
    //刷新UI
    for (NSInteger index = 0; index < self.itemsArray.count; index ++) {
        BaiKeMainClassesDataModel *listModel = self.listModel.sysClassificationList[index];
        UILabel *lab = self.itemsArray[index];
        if (listModel.isSelected) {
            lab.backgroundColor = rgba(255, 240, 240, 1);
            lab.textColor = rgba(255, 66, 66, 1);
            self.preLab = lab;
        } else {
            lab.backgroundColor = rgba(245, 245, 245, 1);
            lab.textColor = rgba(102, 102, 102, 1);
        }
    }
    //刷新UI
    //调整位移
    [self adjustOffsetX];
}
#pragma mark - click
- (void)clickItemLab:(UITapGestureRecognizer *)sender
{
    UILabel *clickLab = (UILabel *)sender.view;
    if (self.preLab == clickLab) {
        return;
    }
    self.preLab.backgroundColor = rgba(245, 245, 245, 1);
    self.preLab.textColor = rgba(102, 102, 102, 1);
    self.listModel.sysClassificationList[self.preLab.tag].isSelected = NO;
    self.preLab = clickLab;
    self.preLab.backgroundColor = rgba(255, 240, 240, 1);
    self.preLab.textColor = rgba(255, 66, 66, 1);
    self.listModel.sysClassificationList[self.preLab.tag].isSelected = YES;
    !self.callBackSelectedModel ?: self.callBackSelectedModel (self.preLab.tag);
    
    //调整位移
    [self adjustOffsetX];
}
#pragma mark - 调整位移
- (void)adjustOffsetX
{
    //调整位移
    CGFloat offsetX = self.preLab.center.x - (self.mainView.width *0.5);
    if (offsetX < 0) {
        offsetX = 0;
    }
    if (self.mainView.contentSize.width > self.mainView.width) {
        CGFloat maxOffsetX = self.mainView.contentSize.width - self.mainView.width;
        if (offsetX > maxOffsetX) {
            offsetX = maxOffsetX;
        }
    }
    [self.mainView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}
#pragma mark - lazy懒加载
- (NSMutableArray<UILabel *> *)itemsArray
{
    if (!_itemsArray) {
        _itemsArray = [NSMutableArray array];
    }
    return _itemsArray;
}
- (UIView *)lineView
{
    if (!_lineView) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = kLineColor;
        view.frame = CGRectMake(14*ADAPTER_WIDTH, self.height - 1, self.width - 14*ADAPTER_WIDTH, 1);
        [self addSubview:view];
        _lineView = view;
    }
    return _lineView;
}
- (UIScrollView *)mainView
{
    if (!_mainView) {
        UIScrollView *view = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height)];
        [self addSubview:view];
        view.backgroundColor = WhiteColor;
        _mainView = view;
    }
    return _mainView;
}
@end
